package cn.com.thingo.hyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.model.bean.GatewayListBean;

public class GatewayListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<GatewayListBean.DataBean> gatewayList;
    private Context context;
    private static final int ADD = 0;
    private static final int LIST = 1;
    private OnItemClickListener listener;


    public GatewayListAdapter(List<GatewayListBean.DataBean> gatewayList, Context context) {
        this.gatewayList = gatewayList;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == gatewayList.size()) {
            return ADD;
        } else {
            return LIST;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == LIST) {
            return new GatewayViewHolder(LayoutInflater.from(context).inflate(R.layout.item_gateway_list, parent, false));
        } else {
            return new AddGatewayViewHolder(LayoutInflater.from(context).inflate(R.layout.item_gateway_add, parent, false));

        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == ADD) {
            if (listener != null) {
                ((AddGatewayViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.addGateway();
                    }
                });
            }
        } else {
            ((GatewayViewHolder) holder).tvName.setText(gatewayList.get(position).getTitle());
            ((GatewayViewHolder) holder).tvNum.setText(gatewayList.get(position).getNumber());
            ((GatewayViewHolder) holder).tvSelect.setVisibility(gatewayList.get(position).isStatus() ? View.VISIBLE : View.GONE);
            if (listener != null) {
                ((GatewayViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onItemClick(position);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return gatewayList.size() + 1;
    }

    private static class AddGatewayViewHolder extends RecyclerView.ViewHolder {

        private AddGatewayViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class GatewayViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.gateway_list_name)
        TextView tvName;
        @BindView(R.id.gateway_list_num)
        TextView tvNum;
        @BindView(R.id.gateway_list_select)
        ImageView tvSelect;


        private GatewayViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void addGateway();
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
